package com.cuje.coolrtc.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

import com.cuje.coolrtc.utils.ColorUtil;

public class GameView extends View {
    //记录小球上面显示的名字
    private String name;
    private boolean isSelf;
    //记录小球的XY坐标
    private float currentX = 200;
    private float currentY = 200;
    //定义、并创建画笔
    Paint paint;
    private int myColor = ColorUtil.randomColor();
    public GameView(Context context, String name, boolean isSelf) {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(30);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);
        this.name = name;
        this.isSelf = isSelf;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(myColor);
        canvas.drawCircle(currentX, currentY, 100, paint);
        paint.setColor(Color.WHITE);
        canvas.drawText(name, currentX, currentY + 15, paint);
    }
    public void setXY(float x, float y) {
        if (!isSelf) {
            this.currentX = x;
            this.currentY = y;
            invalidate();
        }
    }
    public float getX() {
        return this.currentX;
    }
    public float getY() {
        return this.currentY;
    }
    public int getMyColor() {
        return myColor;
    }
    public void setMyColor(int myColor) {
        if (this.myColor != myColor) {
            this.myColor = myColor;
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isSelf) {
            this.currentX = event.getX();
            this.currentY = event.getY();
            invalidate();
            return true;
        } else {
            return false;
        }
    }
}